//
//  FCEmployeeListTableViewCell.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/21.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCRelationBindEmployeeListTableViewCell.h"
#import "FCRelationBindEmployeeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CustomerRelationHeader.h"

@interface FCRelationBindEmployeeListTableViewCell () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) NSArray *employeesArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation FCRelationBindEmployeeListTableViewCell
#define cumtomRowHeight 78

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier employees:(NSArray<FCRelationRequestEmployeeModel *> *)employees{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.employeesArr = employees;
        
        self.backgroundColor = UIColor.clearColor;
        _view = [UIView new];
        [self addSubview:_view];
        [_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.top.and.bottom.mas_equalTo(0);
        }];
        _view.backgroundColor = UIColor.whiteColor;
        
        UILabel *titleLabel = [UILabel new];
        [_view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(12);
            make.left.mas_equalTo(16);
        }];
        titleLabel.font = Bfont17;
        titleLabel.textColor = FCTxtColor;
        titleLabel.text = @"";
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_view addSubview:_tableView];
        WJCWeakSelf
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).mas_equalTo(0);
            make.left.and.right.and.bottom.mas_equalTo(0);
            make.height.mas_equalTo(cumtomRowHeight * weakSelf.employeesArr.count);
            if (weakSelf.employeesArr.count == 0) {
                make.bottom.mas_equalTo(-12);
            }
        }];
        [self setupTableView];
    }
    
    return self;
}

#pragma mark - tableView
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = FCBackColor;
    self.tableView.rowHeight = cumtomRowHeight;
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    self.tableView.scrollEnabled = NO;
    [self setExtraCellLineHidden:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.employeesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FCRelationRequestEmployeeModel *model = self.employeesArr[indexPath.row];
    FCEmployeeModel *employee = model.employee;
    
    FCRelationBindEmployeeTableViewCell *cell = [[FCRelationBindEmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:employee.PortraitUrl] placeholderImage:headPlaceImage];
    cell.nameLabel.text = employee.Name ?: @"";
    if (employee.CompanyDepartment.Name && employee.JobTitle) {
        cell.departmentLabel.text = [NSString stringWithFormat:@"%@-%@",employee.CompanyDepartment.Name ?: @"",employee.JobTitle ?: @""];
    } else{
        cell.departmentLabel.text = [NSString stringWithFormat:@"%@%@",employee.CompanyDepartment.Name ?: @"",employee.JobTitle ?: @""];
    }
    
    return cell;
}

- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    BA_ViewBorderRadius(_view, 4, 0.5, FCEnableColor);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
